/*
 * dive.c
 *
 * Created: 19.04.2014 19:35:32
 *  Author: markus.hufschmid
 */ 

#include "defines.h"
#include "dive.h"
#include "OLED.h"
#include <stdio.h>

int8_t old_deco_state = -1;


void prepare_dive_screen(void){
	Fill_RAM(0x00,0x00);
	draw_horizontal_line(0,0,127,LT_GREY);
	draw_horizontal_line(0,127,127,LT_GREY);
	draw_vertical_line(0,0,127, LT_GREY);
	draw_vertical_line(127,0,127,LT_GREY);
	//Deco Info
	draw_horizontal_line(0,93,127,LT_GREY);
	print_string_zoomed("Deco Info",2,95,LT_GREY, BLACK, 1);
	print_string_zoomed("min",109,117,GREEN, BLACK, 1);
	print_string_zoomed("m",64,117,GREEN, BLACK, 1);
	//Depth
	draw_vertical_line(104,1,92,LT_GREY);
	draw_horizontal_line(1,50,103,LT_GREY);
	print_string_zoomed("Depth",2,52,LT_GREY, BLACK, 1);
	print_string_zoomed("m",98,82,GREEN, BLACK, 1);
	//Time
	draw_horizontal_line(1,23,103,LT_GREY);
	print_string_zoomed("Dive Time",2,25,LT_GREY, BLACK, 1);
	print_string_zoomed("min",38,41, GREEN, BLACK, 1);
	draw_vertical_line(56,24,50,LT_GREY);
	//Temp
	print_string_zoomed("Temp",58,25,LT_GREY, BLACK, 1);
	print_string_zoomed("^C",92,41, GREEN, BLACK, 1);
	//Vario
	print_string_zoomed(">",106, 42, LT_GREY, BLACK, 1);
	print_string_zoomed("<",121, 42, LT_GREY, BLACK, 1);
	draw_horizontal_line(110,45,121,LT_GREY);
	print_string_zoomed(">",106, 21, RED, BLACK, 1);
	print_string_zoomed("<",121, 21, RED, BLACK, 1);
	old_deco_state = -1;
}

void update_dive_screen(deco_info_t deco_info, int depth, int dive_time_min, int8_t temperature, int16_t vario_height){
	char printString[12];
	
	if (old_deco_state != deco_info.no_deco_flag){
		Fill_Block(1,126,103,126,0x00, 0x00);
		if (deco_info.no_deco_flag){
			Fill_Block(2, 44, 103, 124, 0x07, 0xE0);
			print_string_zoomed("No Deco",3,110,BLACK, GREEN, 1);
			print_string_zoomed("min",109,117,GREEN, BLACK, 1);
			} else {
			Fill_Block(2, 27, 103, 124, 0xF8, 0x00);
			print_string_zoomed("Deco",3,110,WHITE, RED, 1);
			print_string_zoomed("min",109,117,RED, BLACK, 1);
			print_string_zoomed("m",64,117,RED, BLACK, 1);
		}
		old_deco_state = deco_info.no_deco_flag;
	}
	//Depth
	sprintf(printString,"%2d.%1d",depth/10, abs(depth)%10);
	print_string_zoomed(printString,2,61,GREEN, BLACK, 4);
	//Deco Info
	if (deco_info.no_deco_flag){
		if (deco_info.no_deco_time_minutes > 99){
			print_string_zoomed(">99",55,103, GREEN, BLACK, 3);
			} else {
			sprintf(printString,"%3d",deco_info.no_deco_time_minutes);
			if (deco_info.no_deco_time_minutes == 0){
				print_string_zoomed(printString,55,103,YELLOW, BLACK, 3);
				} else {
				print_string_zoomed(printString,55,103,GREEN, BLACK, 3);
			}
		}
		} else {
		sprintf(printString,"%2d",deco_info.deco_depth_meters);
		print_string_zoomed(printString,28,103,RED, BLACK, 3);
		sprintf(printString,"%2d",deco_info.deco_time_minutes);
		print_string_zoomed(printString,73,103,RED, BLACK, 3);
	}
	//Time
	sprintf(printString,"%3d",dive_time_min);
	print_string_zoomed(printString,2,34,GREEN, BLACK, 2);
	//Temp
	sprintf(printString,"%2d",temperature);
	print_string_zoomed(printString,68,34,GREEN, BLACK, 2);
	//Vario
	if (vario_height <= 0){
		if (vario_height < -43){
			vario_height = -43;
		}
		Fill_Block(111,120,2, 44 + vario_height, 0x00, 0x00);
		if (vario_height >= -20){
			Fill_Block(111,120,45 + vario_height, 44, 0x1F, 0xE0);
			Fill_Block(111,120,46, 90, 0x00, 0x00);
		} else {
			Fill_Block(111,120,45 + vario_height, 44, 0xF7, 0x00);
			Fill_Block(111,120,46, 90, 0x00, 0x00);
		}
	} else {
		if (vario_height > 43){
			vario_height = 43;
		}
		Fill_Block(111,120,2, 44, 0x00, 0x00);
		Fill_Block(111,120,46, 45 + vario_height, 0x1F, 0xE0);
		Fill_Block(111,120,46 + vario_height, 90, 0x00, 0x00);
		
	}
}

void surfaced(float max_depth, int duration_of_dive){
	char printString[12];
	
	Fill_RAM(0x00, 0x00);
	print_string_zoomed("SURFACED",16,7,LT_GREY, BLACK, 2);
	draw_horizontal_line(0,31,127,LT_GREY);
	Draw_Rectangle(0x00,Max_Column,0x3F,0x5F,0x01,0xC6,0x18);
	show_string_zoomed("Max. Depth",0xC6,0x18,0x02,0x41,0x01);
	Draw_Rectangle(0x00,Max_Column,0x5F,0x7F,0x01,0xC6,0x18);
	show_string_zoomed("Duration",0xC6,0x18,0x02,0x61,0x01);
	sprintf(printString,"%4.1fm",max_depth);
	print_string_zoomed(printString,0x40,0x4C,SKY_BLUE, BLACK, 2);
	sprintf(printString,"%4umin", duration_of_dive/60);
	print_string_zoomed(printString,40,108,SKY_BLUE, BLACK, 2);
}